package com.example.kt_me.ui

import android.content.res.Configuration
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.View.OnClickListener
import androidx.appcompat.app.AppCompatDelegate
import com.blankj.utilcode.util.AppUtils
import com.example.kt_common.base.BaseFragment
import com.example.kt_common.util.AppCommonUitl
import com.example.kt_common.util.ToastUtil
import com.example.kt_me.adapter.BannerAdapter
import com.example.kt_me.bean.Banner
import com.example.kt_mine.R
import com.example.kt_mine.databinding.FragmentMineBinding


class MineFragment : BaseFragment<FragmentMineBinding>(), OnClickListener {
    override fun getLayoutID(): Int {
        return R.layout.fragment_mine
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        init()
    }

    var listBanner: MutableList<Banner> = mutableListOf()
    private fun init() {
        setData()
        mBind.topView.apply {
            setAdapter(BannerAdapter())
            setLifecycleRegistry(lifecycle)
            setScrollDuration(600)
            setInterval(5000)
            setAutoPlay(true)
        }.create()

        mBind.topView.refreshData(listBanner)

        mBind.tvExit.setOnClickListener(this)
        mBind.llVersion.setOnClickListener(this)
        mBind.tvAboutUs.setOnClickListener(this)
        mBind.tvSignOut.setOnClickListener(this)
        mBind.tvVersion.setOnClickListener(this)
        mBind.tvSwitchLanguage.setOnClickListener(this)
        mBind.tvThemeSwitching.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.tv_exit -> {
                ToastUtil.showMsg("退出应用")
                AppUtils.exitApp()
            }

            R.id.tv_version -> {
                ToastUtil.showMsg("APP版本: " + AppUtils.getAppVersionName());
            }

            R.id.tv_about_us -> ToastUtil.showMsg("关于我们")
            R.id.tv_signOut -> {
                ToastUtil.showMsg("切换登录")

            }

            R.id.tv_switch_language -> ToastUtil.showMsg("切换语言")

            R.id.tv_theme_switching -> switchDarkMode()

        }
    }

    private fun setData() {
        listBanner.add(Banner(imageRes = com.example.kt_common.R.drawable.ic_bt_logo))
        listBanner.add(Banner(imageRes = com.example.kt_common.R.drawable.ic_safetravel))
        listBanner.add(Banner(imageRes = com.example.kt_common.R.drawable.ic_greentravel))
        listBanner.add(Banner(imageRes = com.example.kt_common.R.drawable.ic_taxitravel))
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        Log.d(TAG, "onConfigurationChanged: ")
    }

    private fun switchDarkMode() {
        if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
            AppCommonUitl.setAppDarkMode(AppCompatDelegate.MODE_NIGHT_NO)
        } else {
            AppCommonUitl.setAppDarkMode(AppCompatDelegate.MODE_NIGHT_YES)
        }
    }
}